﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Reflection;
using System.Collections.Specialized;

namespace VechilePost
{
    public class HttpPostHelper
    {
        public static string Post(string url, string postData)
        {
            return Post(url, postData, "application/json");
        }

        private static string Post(string url, string postData, string contentType)
        {
            byte[] bytesToPost = System.Text.Encoding.UTF8.GetBytes(postData);

            string responseResult = String.Empty;

            HttpWebRequest req = (HttpWebRequest)
            HttpWebRequest.Create(url);   
            req.Headers.Set("EngineCode", "ad0srrrmsm243b97");
            req.Headers.Set("EngineSecret", "dRARULjYAoCxvsvoDAOt8igdT16fM7agn/3Vj357EofIrXx2Vxinyw==");

            req.Method = "POST";
            req.ContentType = contentType;
            req.ContentLength = bytesToPost.Length;

            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bytesToPost, 0, bytesToPost.Length);     
            }
            HttpWebResponse cnblogsRespone = (HttpWebResponse)req.GetResponse();
            if (cnblogsRespone != null && cnblogsRespone.StatusCode == HttpStatusCode.OK)
            {
                StreamReader sr;
                using (sr = new StreamReader(cnblogsRespone.GetResponseStream()))
                {
                    responseResult = sr.ReadToEnd(); 
                }
                sr.Close();
            }
            cnblogsRespone.Close();
            return responseResult;
        }

    }
}
